package com.mozq.ssm.config.shiro;

import lombok.Data;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class MyRealm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        User user = (User) principalCollection.getPrimaryPrincipal();
        System.out.println(user);
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo();
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        if (authenticationToken instanceof MyToken){
            MyToken token = (MyToken) authenticationToken;
            System.out.println(token.getStoreId());
            System.out.println(token.getTelephone());
            User user = new User();
            user.setName("刘备");
            user.setTelephone(token.getTelephone());
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, null, "myRealm");
            return info;
        }
        return null;
    }

    @Data
    class  User{
        private String name;
        private String telephone;
    }

    @Override
    public boolean supports(AuthenticationToken token){
        boolean flag = super.supports(token);
        if(!flag && token instanceof MyToken){
            return true;
        }
        return false;
    }
}
